﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Management;

namespace UDPReceive
{
    class Program
    {
     
        static void Main(string[] args)
        {
          

              UdpClient sock;
              int port = 89;
              IPEndPoint ep;
            try
            {
                sock = new UdpClient(port);
                ep = new IPEndPoint(0, 89);
                do
                {
                    byte[] recData = sock.Receive(ref ep);
                    Console.WriteLine(Encoding.Default.GetString(recData));
                    Console.WriteLine(sock.Client.AddressFamily.ToString());
                    Console.WriteLine(sock.Client.LocalEndPoint.AddressFamily.ToString());

                    IPHostEntry host;
                    string localIP = "";
                    host = Dns.GetHostEntry(Dns.GetHostName());


                    foreach (IPAddress ipAdd in host.AddressList)
                    {

                        if (ipAdd.AddressFamily == AddressFamily.InterNetwork) 
                        {
                            localIP = ipAdd.ToString();
                          
                        }
                       
                           

                      
                    }
                    Console.WriteLine(localIP);

                    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    string MACAddress = "";
                    foreach (ManagementObject mo in moc)
                    {
                        
                        if (mo["MacAddress"] != null)
                        {
                            if ((bool)mo["IPEnabled"] == true)
                            {
                                MACAddress = mo["MacAddress"].ToString();
                                Console.WriteLine(MACAddress);
                            }
                        }
                    }






                    /*
                    int j = 0;
                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                 

                    PhysicalAddress address = nics[j].GetPhysicalAddress();

                    byte[] bytes = address.GetAddressBytes();

                    for (int i = 0; i < bytes.Length; i++)
                    {
                        Console.Write("{0}", bytes[i].ToString("X2"));

                        if (i != bytes.Length - 1)
                        {
                            Console.Write("-");
                        }
                    }

                    Console.ReadLine();*/

                }
                while (true);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message.ToString());
            }
            
 
        }
    }
}
